package com.kfm.base.it;

import java.util.*;

/**
 * 类实现了某某接口，重写xxx方法
 * 类继承了某某类, 重写xxx方法
 */
public class MyList implements List {

    // 存元素的数组
    private Object[] value;

    // 下一个元素的下标， 元素个数
    private int size;

    // 每次增长量
    private int increment;

    // 默认长度
    private static final int DEFAULT_CAPACITY = 5;

    // 默认增长量
    private static final int DEFAULT_INCREMENT = 10;


    public MyList(){
        // 调用
        this(DEFAULT_CAPACITY, DEFAULT_INCREMENT); // this(5, 10); --> MyList(int, int)
//         this([参数列表]);
    }

    public MyList(int capacity){
        this(capacity, DEFAULT_INCREMENT);
    }

    public MyList(int capacity, int increment){
        if (capacity < 0 ){
            capacity = DEFAULT_CAPACITY;
        }
        if (increment <= 0){
            increment = DEFAULT_INCREMENT;
        }
        // 初始化字段
        this.value = new Object[capacity];
        this.increment = increment;
    }

    @Override
    public int size() {
        return this.size;
    }

    @Override
    public boolean isEmpty() {
        return this.size == 0;
    }

    @Override
    public boolean contains(Object o) {
        return false;
    }


    @Override
    public Object[] toArray() {
        // 把存的元素复制到新数组
        Object[] arr = Arrays.copyOf(value, size);
        return arr;
    }

    @Override
    public boolean add(Object o) {
        grow(size + 1);

        // 存元素
        value[size] = o;
        size ++;
        return true;
    }

    /**
     * 扩容
     * @param minCapacity 所需大小
     */
    private void grow(int minCapacity) {
        // 所需空间 大于 总长度
        if (minCapacity > value.length){
            // 扩容
            int old = value.length;
            int newLength = old + increment;

            // 把元素复制到新数组中
            value = Arrays.copyOf(value, newLength);
        }
    }

    @Override
    public boolean remove(Object o) {
        return false;
    }


    @Override
    public void clear() {
//        value = new Object[]{};
        for (int i = 0; i < size; i++) {
            value[i] = null;
        }
        size = 0;
    }

    @Override
    public Object get(int index) {
        if (index >= 0 && index < size){
            return value[index];
        }
        return null;
    }

    @Override
    public Object set(int index, Object element) {
        return null;
    }

    @Override
    public void add(int index, Object element) {

    }

    @Override
    public Object remove(int index) {
        return null;
    }

    @Override
    public int indexOf(Object obj) {
        // 遍历已存元素
        for (int i = 0; i < size; i++) {
            Object current = value[i]; // 当前元素
            if (current == null){
                if (current == obj){ // obj == null
                    // 找到了
                    return i;
                }
            } else {
                if (current.equals(obj)){
                    // 找到
                    return i;
                }
            }

        }
        return -1;

    }

    @Override
    public int lastIndexOf(Object o) {
        return 0;
    }

    @Override
    public List subList(int fromIndex, int toIndex) {
        return null;
    }

    // --------------------------------------------------


    @Override
    public boolean addAll(Collection c) {
        return false;
    }

    @Override
    public boolean addAll(int index, Collection c) {
        return false;
    }

    @Override
    public ListIterator listIterator() {
        return null;
    }

    @Override
    public ListIterator listIterator(int index) {
        return null;
    }

    @Override
    public boolean retainAll(Collection c) {
        return false;
    }

    @Override
    public boolean removeAll(Collection c) {
        return false;
    }

    @Override
    public boolean containsAll(Collection c) {
        return false;
    }

    @Override
    public Object[] toArray(Object[] a) {
        return new Object[0];
    }

    @Override
    public Iterator iterator() {
        return null;
    }

}
